#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

#define LOOP 10000000
#define NUM 4000  
static int64_t x = 0;

pthread_mutex_t mutex;


void *thread(void *argc){
    // pthread_mutex_lock(&mutex);
    for(int i = 0; i < LOOP; i++){
        pthread_mutex_lock(&mutex);
        x++;
        pthread_mutex_unlock(&mutex);
    }
    // pthread_mutex_unlock(&mutex);
}


int main(){

    pthread_t tid[NUM];
    pthread_mutex_init(&mutex, NULL);

    for(int i = 0; i < NUM; i++){
      pthread_create(&(tid[i]), NULL, thread, NULL);
    }
    for(int i=0; i< NUM; i++){
      pthread_join(tid[i], NULL);
    }
    printf("x = %ld\n", x);

    return 0;
}